#!/usr/bin/env python
# -*- coding:utf8 -*-
"""
Author : mafei
@Date   : 2020/01/05

"""
import time
import base64
import requests
import hashlib
import hmac
import random


def captcha_verify(verify):
    return
    secret_id = ""
    secret_key = ""

    def get_string_to_sign(method, endpoint, params):
        s = method + endpoint + "/?"
        query_str = "&".join("%s=%s" % (k, params[k]) for k in sorted(params))
        return s + query_str

    def sign_str(key, s, method):
        hmac_str = hmac.new(key.encode("utf8"), s.encode("utf8"), method).digest()
        return base64.b64encode(hmac_str)

    data = {
        "Action": "DescribeCaptchaResult",
        "CaptchaType": 9,
        "CaptchaAppId": verify['appid'],
        "AppSecretKey": "0dTyLz3LUrMDhhtd6Y392UA**",
        "Ticket": verify['ticket'],
        "Randstr": verify['randstr'],
        "UserIp": verify['UserIP'],
        "Version": "2019-07-22",
        "Timestamp": int(time.time()),
        "Nonce": random.randrange(1, 10000),
        "SecretId": secret_id
    }
    endpoint = "captcha.tencentcloudapi.com"
    s = get_string_to_sign("GET", endpoint, data)
    data["Signature"] = sign_str(secret_key, s, hashlib.sha1)
    resp = requests.get("https://" + endpoint, params=data)
    return resp.json().get("retcode")